package com.kidedu.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.ibatis.session.RowBounds;

import net.sf.json.JSONObject;

import com.kidedu.dao.mapper.KGradeMapper;
import com.kidedu.dao.mapper.KOrderLinkedKGradeMapper;
import com.kidedu.dao.mapper.KOrderMapper;
//import com.kidedu.dao.mapper.OrderGradeLinkedMapper;
import com.kidedu.entity.KGrade;
import com.kidedu.entity.KOrder;
import com.kidedu.entity.KOrderExample;
import com.kidedu.entity.dto.KOrderDTO;
//import com.kidedu.entity.dto.KOrderDTO;
import com.kidedu.service.IKOrderService;
import com.kidedu.util.BaseService;
import com.kidedu.util.Pagination;

public class KOrderService extends BaseService implements IKOrderService{

	@Resource
	KOrderMapper kOrderMapper;
	@Resource
	KGradeMapper  kGraderMapper;
	@Resource
	KOrderLinkedKGradeMapper  kOrderLinkedKGradeMapper;
	/*@Resource
	OrderGradeLinkedMapper orderGradeLinkedMapper;*/
	public boolean AddNewOrder(String course_id, String Stu_id) {
		// TODO Auto-generated method stub
		   KGrade KGrade=kGraderMapper.selectByPrimaryKey(course_id);
			KOrder Order=new KOrder();
			Order.setOsid(Stu_id);
			Order.setOgid(course_id);
			Order.setOfee(KGrade.getGexpress());
			Order.setOstate(false);
		try{
			kOrderMapper.insertSelective(Order);
			return true;
		}
		catch(Exception e)
		{
			return false;
		}
	
	
	}
	public boolean deleteOrder(List<Byte> Order_id) {
		// TODO Auto-generated method stub
		return false;
	}
	public JSONObject OrderListFY(int pageNO, int PageSize) {
		// TODO Auto-generated method stub
		return null;
	}
	public boolean payOrder(Byte Order_id) {
		// TODO Auto-generated method stub
		return false;
	}

	
	/**
	 * 根据学生id查询订单
	 */
	public List<KOrder> selectKOrderListByOsid(String osid) {
		KOrderExample example = new KOrderExample();
		example.createCriteria().andOsidEqualTo(osid);
		return kOrderMapper.selectByExample(example);
	}
	
	
	public Pagination<KOrderDTO> MyOrderList(String Stu_Id, int pageNo,
			int PageSize) {
		// TODO Auto-generated method stub
		KOrderExample example = new KOrderExample();
		example.createCriteria().andOsidEqualTo(Stu_Id).andOstateEqualTo(false);
		int count=kOrderMapper.countByExample(example);
		Map<String, Object> inParamMap =new HashMap<String, Object>();
		inParamMap.put("osid", Stu_Id);
		
		List<KOrderDTO> orderList=kOrderLinkedKGradeMapper.selectkorderList(inParamMap, new RowBounds(super.calculateStartPage(pageNo, PageSize), PageSize));
		return new Pagination<KOrderDTO>(pageNo, PageSize, count, orderList);
	}
	@Override
	public List<String> selectOrderByStuIdReturnGid(String stuid) {
		// TODO Auto-generated method stub
	
		Map<String, Object> inParamMap =new HashMap<String, Object>();
		inParamMap.put("osid", stuid);
		inParamMap.put("ostate",true);
		List<KOrder> orderList =kOrderMapper.selectPayedOrder(inParamMap);
		List<String> GidList=new ArrayList<String>();
	     for(KOrder order : orderList)
	     {
	    	 GidList.add(order.getOgid());
	     }
		return GidList;
	}
	@Override
	public KOrder findOrder(Byte orderId) {
		// TODO Auto-generated method stub
		return kOrderMapper.selectByPrimaryKey(orderId);
	}
	@Override
	public boolean updatefee(Byte oid, Float fee) {
		// TODO Auto-generated method stub
		KOrder order=kOrderMapper.selectByPrimaryKey(oid);
		if(order.getOfee()!=fee)
			{
			    order.setOfee(fee);
			    order.setOdinner(true);
				kOrderMapper.updateByPrimaryKeySelective(order);
			}	
		return true;
	}
	@Override
	public boolean updateOrder(KOrder order) {
		// TODO Auto-generated method stub
		kOrderMapper.updateByPrimaryKey(order);
		return true;
	}
}
